//
//  HudTextInput.m
//  storeapp
//
//  Created by huangyi on 13-5-22.
//  Copyright (c) 2013年 ios. All rights reserved.
//

#import "HudTextInput.h"
#import "LocalizeMgr.h"
#import "UIView+utils.h"

@interface HudTextInput () <UIAlertViewDelegate>

@property(nonatomic,strong)NSString *textString;
@property(nonatomic,strong)dispatch_block_id block;
@property(nonatomic,assign)UITextView *inputText;

@end

@implementation HudTextInput

/*
-(id)initWithTitle:(NSString*)title andBlock:(dispatch_block_id)code{
    self=[super init];
    if (self) {
        UIAlertView *al=[[UIAlertView alloc] initWithTitle:title message:nil delegate:self cancelButtonTitle:LOC_STR(@"取消") otherButtonTitles:@"确定", nil];
        al.alertViewStyle=UIAlertViewStylePlainTextInput;
        self.alert=al;
        self.block=code;
    }
    return self;
}

-(void)show{
    [self.alert show];
}

- (void)willPresentAlertView:(UIAlertView *)alertView{
    UITextField *textfield=[alertView textFieldAtIndex:0];
    if (textfield) {
        textfield.borderStyle=UITextBorderStyleRoundedRect;
    }
}
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex!=0) {
        if (self.block!=nil) {
            UITextField *textfield=[alertView textFieldAtIndex:0];
            if (textfield) {
                NSString *text=textfield.text;
                self.block(text);
            }
        }
    }
}
*/

+(HudTextInput*)hudWithTitle:(NSString*)title andBlock:(dispatch_block_id)code{
    HudTextInput *hud=[[HudTextInput alloc] init];
    hud.textString=title;
    hud.block=code;
    return hud;
}

-(void)show{
    [super show];
    double delayInSeconds = 0.50;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self.inputText becomeFirstResponder];
    });
}
-(void)hide{
    [super hide];
}

-(void)buildHud{
    CGFloat width=280;
    CGFloat spacing=10;
    CGFloat offset=230;
    CGFloat radius=6;
    UIFont *font=UIFontFromString(@"@16");
    CGSize button=CGSizeMake(80, 30);

    UIView *container=[[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:container];
    self.container=container;
    container.clipsToBounds=YES;
    container.backgroundColor=[UIColor colorWithWhite:0.9 alpha:1];
    container.layer.cornerRadius=radius;
    container.layer.shadowRadius=3;
    container.layer.shadowOpacity=1;
    container.layer.shadowOffset=CGSizeZero;
    container.layer.shadowColor=[UIColor blackColor].CGColor;
    container.autoresizingMask=(UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin);

    UILabel *textView=[[UILabel alloc] initWithFrame:CGRectZero];
    [container addSubview:textView];
    if (self.textString) {
        textView.text=self.textString;
        textView.font=font;
        textView.numberOfLines=0;
        textView.backgroundColor=[UIColor clearColor];
        [textView sizeToFit];
    }
    width=MAX(width, CGRectGetWidth(textView.frame));

    UITextView *textInput=[[UITextView alloc] initWithFrame:CGRectMake(0, 0, width-spacing-spacing, 120)];
    [container addSubview:textInput];
    textInput.font=font;
    textInput.layer.cornerRadius=radius;
    textInput.backgroundColor=[UIColor whiteColor];
    self.inputText=textInput;
    width=MAX(width, CGRectGetWidth(textInput.frame));

    UIView *buttonView=[[UIView alloc] initWithFrame:CGRectZero];
    [container addSubview:buttonView];
    {
        UIButton *buttonObj=[[UIButton alloc] initWithFrame:CGRectZero];
        [buttonView addSubview:buttonObj];
        buttonObj.frame=CGRectMake((button.width+spacing)*0, 0, button.width, button.height);
        [buttonObj setTitle:LOC_STR(@"确定") forState:UIControlStateNormal];
        [buttonObj setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [buttonObj setBackgroundColor:[UIColor whiteColor]];
        buttonObj.titleLabel.font=font;
        buttonObj.layer.cornerRadius=radius;
        buttonObj.tag=0;
        [buttonObj addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        buttonView.frame=CGRectMake(0, 0, CGRectGetMaxX(buttonObj.frame), CGRectGetMaxY(buttonObj.frame));
    }
    {
        UIButton *buttonObj=[[UIButton alloc] initWithFrame:CGRectZero];
        [buttonView addSubview:buttonObj];
        buttonObj.frame=CGRectMake((button.width+spacing)*1, 0, button.width, button.height);
        [buttonObj setTitle:LOC_STR(@"取消") forState:UIControlStateNormal];
        [buttonObj setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [buttonObj setBackgroundColor:[UIColor whiteColor]];
        buttonObj.titleLabel.font=font;
        buttonObj.layer.cornerRadius=radius;
        buttonObj.tag=1;
        [buttonObj addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        buttonView.frame=CGRectMake(0, 0, CGRectGetMaxX(buttonObj.frame), CGRectGetMaxY(buttonObj.frame));
    }
    [buttonView sizeToFit];
    width=MAX(width, CGRectGetWidth(buttonView.frame));

    width=spacing+width+spacing;
    textView.frame=CGRectOffset(textView.frame, 0, spacing);
    textView.center=CGPointMake(width/2, textView.center.y);
    textInput.frame=CGRectOffset(textInput.frame, 0, CGRectGetMaxY(textView.frame)+spacing);
    textInput.center=CGPointMake(width/2, textInput.center.y);
    buttonView.frame=CGRectOffset(buttonView.frame, 0, CGRectGetMaxY(textInput.frame)+spacing);
    buttonView.center=CGPointMake(width/2, buttonView.center.y);
    container.frame=CGRectMake(0, 0, width, CGRectGetMaxY(buttonView.frame)+spacing);
    container.center=CGPointMake(CGRectGetWidth(self.view.frame)/2, CGRectGetHeight(self.view.frame)/2-offset);
}

-(void)buttonClicked:(id)sender{
    UIButton *button=sender;
    if (button.tag==0) {
        if (self.block!=nil) {
            NSString *string=self.inputText.text;
            self.block(string);
        }
    }
    [self hide];
}

@end
